#pragma strict
////////////////////////////////////////////////////////////////////////////////////
///朝目标飞行.
////////////////////////////////////////////////////////////////////////////////////
//半径.
var radius:float = 2.5;
//目标.
var target:Transform;
// 距离块[0,1]
var factor:float = 0;
//接近的速度因素.
var speed:float = 1; // [0,1]范围内,factor增长的速度.
//距离为0.25时,结束.
var minDistance:float = 0.25;
/* 事件 */
var eventName:String = "OnFlyToComplete";
var eventReceiver:Transform;
//飞行完毕,自动销毁?
var destroyWhenComplete:boolean = true;

// private var c:SphereCollider;
private var tr:Transform;
private var originalPos:Vector3;

function Start () {
	tr = transform;
	// c = GetComponent.<SphereCollider>();
	// if(! c)
		// c = gameObject.AddComponent.<SphereCollider>();
	// c.radius = radius;
	// c.isTrigger = true;
	
	factor = 0;
	originalPos = tr.position;
}

function Update(){
	FlyTo();
}

function FlyTo(){
	if(target){
		if(factor < 1){
			factor += speed * Time.deltaTime;		
		}
		
		tr.position = tr.position * (1 - factor) + target.position * factor;
		
		if(Vector3.Distance(tr.position,target.position) < minDistance){
			factor = 1;
			FlyComplete();
		}
	}	
}

function FlyComplete(){
	if(eventReceiver){
		eventReceiver.SendMessage(eventName);
	}
	if(destroyWhenComplete){
		Destroy(gameObject);
	}	
}
